/**
 * 自定义函数
 * */


//列表
function getIndex(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var keyword = $('#keyword').val();
    var pageStart = getParamByUrl('start');
    pageStart = pageStart ? pageStart : pages.start;
    // var sort_id = $('#sort_id').val();
    var formData = {
        role_id: uinfo.role_id,
        depart_id: uinfo.depart_id,
        kword: keyword,
        sort_id: sort_id,
        start: pageStart, limit: pages.limit,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'config/item/index',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.start = pageStart;
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var uinfo = getAdminInfo();
    var xuhao, pageStart = getParamByUrl('start');
    pageStart = pageStart ? pageStart : pages.start;
    var h = '', i;
    h += '';
    for (i = 0; i < d.length; i ++) {
        xuhao = (pageStart - 1) * pages.limit + i + 1;
        h += '<tr class="text-c">';
        h += '<td title="' + d[i].id + '">' + xuhao + '</td>';
        h += '<td>' + d[i].title + '</td>';
        h += '<td>' + d[i].starttime + '<br>-<br>' + d[i].endtime + '</td>';
        h += '<td>' + d[i].leader_name + '</td>';
        h += '<td>' + d[i].depart_name + '</td>';
        h += '<td>' + d[i].teamstr + '</td>';
        h += '<td>';
        h += d[i].is_able==0 ? '正常' : '已禁用';
        if (d[i].is_able>0) {
            h += '<br>原因：' + d[i].able_remark;
        }
        h += '</td>';
        h += '<td width="70">' + d[i].lasttime + '</td>';
        if (uinfo.role_id != 2) {
            h += '<td class="td-manage">';
            h += '<a href="javascript:;" class="ml-5" onclick="lay_edit(\'编辑\',\'item-edit.html\', ' + d[i].id + ');">';
            h += '编辑';
            h += '<i class="Hui-iconfont">&#xe6df;</i>';
            h += '</a>';
            h += '&nbsp;&nbsp;';
            h += '<a href="javascript:;" onclick="lay_team_edit(\'编辑\',\'item-team.html\', ' + d[i].id + ');">团队配置</a>';
            h += '&nbsp;&nbsp;';
            if (d[i].is_able == 1) {
                h += '<a href="javascript:;" onclick="able_no(' + d[i].id + ');">禁用</a>';
            } else {
                h += '<a href="javascript:;" onclick="able_yes(' + d[i].id + ');">启用</a>';
            }
            //h += '&nbsp;&nbsp;';
            //h += '<a href="javascript:;" class="ml-5" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
            //h += ' 删除';
            //h += '<i class="Hui-iconfont"></i>';
            //h += '</a>';
            h += '<input type="hidden" id="title' + d[i].id +'" value="' + d[i].title + '"/>';
            h += '<input type="hidden" id="starttime' + d[i].id +'" value="' + d[i].starttime + '"/>';
            h += '<input type="hidden" id="endtime' + d[i].id +'" value="' + d[i].endtime + '"/>';
            h += '<input type="hidden" id="leader_id' + d[i].id +'" value="' + d[i].leader_id + '"/>';
            h += '<input type="hidden" id="depart_id' + d[i].id +'" value="' + d[i].depart_id + '"/>';
            h += '</td>';
        }
        h += '</tr>';
    }
    return h;
}


// 跳转
function toJump(){
    var sort_id = $('#sort_id').val();
    var p = '?sort_id=' + sort_id;
    window.location.href = 'item.html' + p;
}


// 选项
function getOption(){
    var h = '';
    // 排序
    if (sort_id == 2) {
        h = '<option value="1">按日期正序</option>';
        h += '<option value="2" selected>按日期倒序</option>';
    } else {
        h = '<option value="1" selected>按日期正序</option>';
        h += '<option value="2">按日期倒序</option>';
    }
    $('#sort_id').html(h);
}


//编辑弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    var tt = $('#title' + id).val();
    var starttime = $('#starttime' + id).val();
    var endtime = $('#endtime' + id).val();
    var leader_id = $('#leader_id' + id).val();
    tt = tt ? escape(tt) : '';
    starttime = starttime ? escape(starttime) : '';
    endtime = endtime ? escape(endtime) : '';
    leader_id = leader_id ? escape(leader_id) : 0;
    if (starttime == '' || endtime == '') {
        var today = new Date();
        var year = today.getFullYear();
        var month = today.getMonth() + 1;
        var day = today.getDate();
        month = month.length==1 ? '0'+month : month; 
        day = day.length==1 ? '0'+day : day; 
        var day_str = year + '-' + month + day;
        starttime = endtime = day_str;
    }
    url += '&title=' + tt;
    url += '&starttime=' + starttime;
    url += '&endtime=' + endtime;
    url += '&leader_id=' + leader_id;
    layer_show(title,url,w,h);
}


//获取详情
function getOne(){
    if (!id) { return }
    $('#title')[0].value = title;
    var formData = {
        id: id,
    };
    console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'config/item/one',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            var d = res.data.data;
            var leaderList = res.data.leaderList;
            var departList = res.data.departList;
            $('#startday')[0].value = d.starttime;
            $('#endday')[0].value = d.endtime;
            var h = '', i;
            h = '';
            if (d.leader_id == 0) {
                h += '<option value="0" selected>-选择领导-</option>';
            } else {
                h += '<option value="0">-选择领导-</option>';
            }
            for (i = 0; i < leaderList.length; i ++) {
                if (d.leader_id == leaderList[i].id) {
                    h += '<option value="' + leaderList[i].id + '" selected>';
                } else {
                    h += '<option value="' + leaderList[i].id + '">';
                }
                h += leaderList[i].title;
                h += '</option>';
            }
            $('#leader_id').html(h);
            var departList = departList;
            h = '';
            if (d.depart_id == 0) {
                h += '<option value="0" selected>-选择部门-</option>';
            } else {
                h += '<option value="0">-选择部门-</option>';
            }
            for (i = 0; i < departList.length; i ++) {
                if (d.depart_id == departList[i].id) {
                    h += '<option value="' + departList[i].id + '" selected>';
                } else {
                    h += '<option value="' + departList[i].id + '">';
                }
                h += departList[i].title;
                h += '</option>';
            }
            $('#depart_id').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//编辑团队弹窗
function lay_team_edit(title,url,id,w,h){
    url += '?id=' + id;
    //var tt = $('#title' + id).val();
    //tt = tt ? escape(tt) : '';
    //url += '&title=' + tt;
    h = 400;
    layer_show(title,url,w,h);
}


////显示图片
//function showImg(e){
//    var h = '';
//    h += '<img src="' + e.img + '" style="width: 150px;"/>';
//    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
//    $('#bigShow').html(h);
//}


//修改
function modify(){
    var uinfo = getAdminInfo();
    var title = $('#title').val();
    var starttime = $('#startday').val();
    var endtime = $('#endday').val();
    var leader_id = $('#leader_id').val();
    var depart_id = $('#depart_id').val();
    if (title == '') {
        msgPop({msg: '项目名称必须的'}); return
    }
    if (startday == '') {
        msgPop({msg: '开始时间必须的'}); return
    }
    if (endday == '') {
        msgPop({msg: '结束时间必须的'}); return
    }
    var formData = {
        role_id: uinfo.role_id, 
        id: id, title: title,
        starttime: starttime,
        endtime: endtime,
        leader_id: leader_id,
        depart_id: depart_id,
    };
    if (formData.title == '') {
        msgPop({msg: '名称必须的'}); return
    }
    console.log(formData); //return
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'config/item/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                //msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            //window.location.href = '';
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


////删除
//function lay_del(id) {
//    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
//    layer.confirm(msg, function (index) {
//        $.ajax({
//            type: apiMethod,
//            url: apiDomain + 'config/item/del',
//            data: {  id: id },
//            success: function (res) {
//                console.log(res);
//                if (res.code < 0) {
//                    msgPop({msg: res.msg});
//                    return
//                }
//                layer.msg('已删除!', {icon: 1, time: 1000});
//                setTimeout(function () {
//                    top.location.reload();
//                }, 500);
//            },
//            error: function () {
//                console.log('错误了！');
//            }
//        });
//    });
//}


//团队获取
function teamGet(){
    var formData = {
        id: id,
    };
    console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'config/item/team-get',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                //msgPop({msg: res.msg});
                return
            }
            var staffList = res.data.staff;
            var teamList = res.data.team;
            var h = '', i;
            h = '';
            h += '<option value="0">-选择人员-</option>';
            for  (i = 0; i < staffList.length; i ++) {
                h += '<option value="' + staffList[i].id + '">' + staffList[i].title + '</option>';
            }
            $('#staff_id').html(h);
            h = '';
            for (i = 0; i < teamList.length; i ++) {
                h += '<div class="li">';
                h += teamList[i].staff_name;
                h += '<a href="javascript:;" onclick="teamDel(' + teamList[i].id + ');">移除</a>';
                h += '</div>';
            }
            $('#teamList').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//团队设置
function teamSet(v){
    var formData = {
        id: id, staff_id: v,
    };
    console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'config/item/team-set',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            teamGet();
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//团队移除
function teamDel(id){
    var formData = {
        id: id,
    };
    console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'config/item/team-del',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            layer.msg('已移除!', {icon: 1, time: 1000});
            teamGet();
            //setTimeout(function () {
            //    top.location.reload();
            //}, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//禁用弹窗
function able_no(v){
    layer.prompt({
        fromType: 0,
        value: '',
        title: '请输入禁用的原因',
        bnt: ['确定', '取消'],
        btnAlign: 'c'
    }, function(val, index){
        //var remark = val;
        layer.close();
        var formData = {
            id: v, able: 2, remark: val,
        };
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'config/item/able',
            data: formData,
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('成功', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//禁用弹窗
function able_yes(v){
    var msg = '确认要 <b style="color: red; font-size: 20px;">启用</b> 吗？';
    layer.confirm(msg, function (index) {
        var formData = {
            id: v, able: 1,
        };
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'config/item/able',
            data: formData,
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('成功', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}